<!--电子围栏页 GeofenceView.vue-->
<template>
  <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;">围栏管理</span>
    <el-button type="primary" style="float:right;margin-top:13px;" @click="router.push('/geofenceMap')">新建围栏</el-button>
  </div>
  <!-- 围栏查询卡片 -->
  <el-card style="margin:20px;height: 70px;">
    <el-form :inline="true">
      <el-form-item label="围栏名称">
        <el-input placeholder="请输入" style="width:220px;" v-model="geoSearchForm.name"
                  @keydown.enter="loadGeo"></el-input>
      </el-form-item>
      <el-form-item label="围栏状态" style="width:290px;">
        <el-select placeholder="请选择" v-model="geoSearchForm.status" @change="loadGeo">
          <el-option label="启用" value="1"/>
          <el-option label="禁用" value="0"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetSearch">重置</el-button>
        <el-button type="primary" @click="loadGeo">查询</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <!-- 围栏表格数据 -->
  <el-card style="margin:20px;">
    <el-table stripe :data="geoTableData">
      <el-table-column label="编号" prop="id" type="index" align="center" width="100"></el-table-column>
      <el-table-column label="围栏名称" prop="name" align="center"></el-table-column>
      <el-table-column label="围栏坐标点" prop="position" align="center" show-overflow-tooltip="true"
                       width="500"></el-table-column>
      <el-table-column label="创建时间" prop="createTime" align="center"></el-table-column>
      <el-table-column label="围栏状态" align="center">
        <template #default="scope">
          <el-switch inactive-value="0" active-value="1" v-model="scope.row.status"
                     @click="changeStatus(scope.row.id,scope.row.status)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button type="primary" link @click="loadBindVehicle(scope.row.id)">管理车辆</el-button>
          <el-button type="danger" link @click="deleteGeo(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
  <!-- 已绑定车辆弹窗 -->
  <el-dialog title="已绑定车辆" v-model="bindVehicleDialogVisible" :row-id="geofenceId" style="padding:40px;">
    <el-button type="primary" style="margin:10px 0;" @click="loadNotBindVehicle">添加绑定车辆</el-button>
    <el-table :data="bindVehicleArr">
      <el-table-column prop="brand" label="车辆品牌" align="center"></el-table-column>
      <el-table-column prop="license" label="车牌号" align="center"></el-table-column>
      <el-table-column prop="type" label="车辆类型" align="center" :formatter="vehicleTypeFormat"></el-table-column>
      <el-table-column
          label="操作"
          fixed="right"
          width="200"
          align="center">
        <template #default="scope">
          <el-button link type="primary" @click="unBindGeofence(scope.row.id)">移除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-dialog>
  <!-- 添加可绑定车辆列表弹窗 -->
  <el-dialog title="可绑定车辆列表" v-model="AddBindVehicleDialogVisible" :before-close="handleInnerClose">
    <el-table :data="notBindVehicleArr">
      <el-table-column label="编号" type="index" align="center" width="80px"></el-table-column>
      <el-table-column prop="brand" label="车辆品牌" align="center"></el-table-column>
      <el-table-column prop="license" label="车牌号" align="center"></el-table-column>
      <el-table-column prop="type" label="车辆类型" align="center" :formatter="vehicleTypeFormat"></el-table-column>
      <el-table-column prop="status" label="车辆状态" align="center"></el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" @click="BindGeofence(scope.row.id)">绑定</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-dialog>
</template>
<script setup>
import {onMounted, ref} from "vue";
import axios from "axios";
import {ElMessage} from "element-plus";
import qs from "qs";
import router from "@/router";

//控制"已绑定车辆弹窗"是否显示
const bindVehicleDialogVisible = ref(false);
//控制"可绑定车辆列表"是否显示
const AddBindVehicleDialogVisible = ref(false);

// 定义变量，保存围栏数据的搜索条件
const geoSearchForm = ref({
  name: '',
  status: ''
});

// 定义数组，用于保存围栏表格数据
const geoTableData = ref([]);

// 定义加载围栏的方法
const loadGeo = () => {
  let data = qs.stringify(geoSearchForm.value)
  console.log(data);
  axios.get(BASE_URL + '/v1/geofence/select?' + data)
      .then((res) => {
        if (res.data.code === 2000) {
          console.log(res.data.data);
          geoTableData.value = res.data.data;
        } else {
          ElMessage.error(res.data.msg);
        }
      })

}

// 定义一进入页面就立即执行的方法
onMounted(() => {
  loadGeo();
})

// 定义重置搜索的方法
const resetSearch = () => {
  geoSearchForm.value = {};
  loadGeo();
}

// 定义变量储存围栏的id
const selectGid = ref('');
// 定义数组储存当前围栏上已绑定车辆信息
const bindVehicleArr = ref([]);
// 定义查看已绑定车辆的方法
const loadBindVehicle = (gid) => {
  selectGid.value = gid
  bindVehicleDialogVisible.value = true;
  axios.get(BASE_URL + '/v1/vehicle/select?geofenceId=' + gid)
      .then((res) => {
        if (res.data.code == 2000) {
          bindVehicleArr.value = res.data.data;
        } else {
          ElMessage.error(res.data.msg);
        }
      })
}
// 定义解除车辆围栏绑定的方法
const unBindGeofence = (vid) => {
  if (confirm("您确认要移除已绑定的车辆吗？该操作不可逆！")) {
    axios.post(BASE_URL + '/v1/vehicle/unbindGeofence/' + vid)
        .then((response) => {
          if (response.data.code === 2000) {
            ElMessage.success('解绑成功！');
            if (selectGid.value != null) {
              loadBindVehicle(selectGid.value);
            }
          } else {
            ElMessage.error(response.data.msg);
          }
        })
  }
}

// 定义数组，用于保存可绑定车辆的数据
const notBindVehicleArr = ref([]);
// 定义查看未绑定车辆的方法
const loadNotBindVehicle = () => {
  AddBindVehicleDialogVisible.value = true;
  axios.get(BASE_URL + '/v1/vehicle/select?geofenceBindStatus=0')
      .then((res) => {
        if (res.data.code == 2000) {
          notBindVehicleArr.value = res.data.data;
        } else {
          ElMessage.error(res.data.msg);
        }
      })
}
// 定义设置车辆围栏绑定的方法
const BindGeofence = (vid) => {
  if (confirm("您确认要将该车辆与此围栏绑定吗？")) {
    axios.post(BASE_URL + '/v1/vehicle/bindGeofence/' + vid + '/' + selectGid.value)
        .then((response) => {
          if (response.data.code === 2000) {
            ElMessage.success('绑定成功！');
            loadNotBindVehicle();
          } else {
            ElMessage.error(response.data.msg);
          }
        })
  }
}
// 内层弹窗的关闭方法
const handleInnerClose = () => {
  AddBindVehicleDialogVisible.value = false;
  if (selectGid.value != null) {
    loadBindVehicle(selectGid.value);
  }
}

// 车辆类型格式化
const vehicleTypeFormat = (row, column, cellValue, index) => {
  switch (row.type) {
    case '10':
      cellValue = '轿车';
      break;
    case '20':
      cellValue = '客车';
      break;
    case '30':
      cellValue = '货车';
      break;
    case '40':
      cellValue = '挂车';
      break;
    default:
      cellValue = '未知';
      break;
  }
  return cellValue;
}

// 定义修改围栏状态的方法
const changeStatus = (gid, status) => {
  console.log(gid);
  console.log(status);
  axios.post(BASE_URL + '/v1/geofence/update/status/' + gid + '/' + status)
      .then((response) => {
        if (response.data.code == 2000) {
          ElMessage.success('状态修改成功！');
          loadGeo();
        } else {
          ElMessage.error(response.data.msg);
        }
      })
}

// 定义删除围栏的方法
const deleteGeo = (gid) => {
  if (confirm('您确定要删除围栏吗？该操作不可逆！')) {
    axios.delete(BASE_URL + '/v1/geofence/delete/' + gid)
        .then((response) => {
          if (response.data.code == 2000) {
            ElMessage.success('删除围栏成功！');
            // axios.post(BASE_URL + '/v1/vehicle/unbindAllThisGid/' + gid)
            //     .then((resUnbind) => {
            //       if (resUnbind.data.code == 2000) {
            //         ElMessage.success('成功解除与该围栏绑定的全部车辆！')
            //       } else {
            //         ElMessage.error(response.data.msg);
            //       }
            //     })
            loadGeo();
          } else {
            ElMessage.error(response.data.msg);
          }
        })
  }
}
</script>

<style scoped>
</style>